package com.lzx.iteam.net;

import android.content.Context;
import android.os.Message;
import android.util.Log;
import com.lzx.iteam.bean.AttendanceData;
import com.lzx.iteam.provider.CloudDBOperation;
import com.lzx.iteam.util.Constants;
import com.lzx.iteam.util.StringUtil;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Megan on 16/4/11.
 */
public class CreateAttendanceMsg extends CcMsgStructure {

    private static final String TAG = "CreateAttendanceMsg";

    private Message mmCallback;
    private Context mContext;
    private CloudDBOperation cloudDBOperation;
    private AttendanceData attendanceData;

    public CreateAttendanceMsg(Message callback, Context context) {
        mmCallback = callback;
        mContext = context;
        cloudDBOperation = new CloudDBOperation(mContext);
    }

    @Override
    public void onReceive(JSONObject data) {
        attendanceData = new AttendanceData();
        try {
            JSONObject jsonObject = data.getJSONObject(AsynHttpClient.KEY_CC_DATA);
            attendanceData.set_id(jsonObject.getString("attendance_form_id"));
            if (jsonObject.has("is_next_day_take_effect")) {
                attendanceData.setNextDayTakeEffect((jsonObject.getInt("is_next_day_take_effect") == 1) ? true : false);
            }
            if (jsonObject.has("sub_forms")){
                attendanceData.setSubForms(jsonObject.getString("sub_forms"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if (mmCallback != null) {
            mmCallback.obj = attendanceData;
            mmCallback.arg1 = 0;
            mmCallback.sendToTarget();
            return;
        }

    }

    @Override
    public void onError(int error, Object ret) {
        Log.d(TAG, "onError : " + error + " ret: " + ret);
        String errorMsg = null;
        HashMap<String, String> backData = new HashMap<String, String>();
        if (ret != null) {
            JSONObject jData = null;
            try {
                jData = new JSONObject(ret.toString());
                if (error == 5001) {
                    backData.put("msg", jData.getString("msg"));
                    backData.put("data", jData.getString("data"));
                } else {
                    errorMsg = jData.getString("msg");
                }
            } catch (JSONException e) {
                errorMsg = "服务端数据解析失败!";
                e.printStackTrace();
            }

        }
        if (mmCallback != null) {
            mmCallback.arg1 = error;
            if (error == 5001) {
                mmCallback.obj = backData;
            } else {
                if (!StringUtil.isEmpty(errorMsg)) {
                    mmCallback.obj = errorMsg;
                } else {
                    mmCallback.obj = Constants.NETWORK_WRROR;
                }
            }
            mmCallback.sendToTarget();
        }

    }
}